/**  
* @Title: CrShopOrderService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月26日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.mapper.cityRun.CrShopGoodsMapper;
import com.lemon.mapper.cityRun.CrShopOrderCommentMapper;
import com.lemon.mapper.cityRun.CrShopOrderGoodsMapper;
import com.lemon.mapper.cityRun.CrShopOrdersMapper;
import com.lemon.mapper.cityRun.CrShopUserMapper;
import com.lemon.mapper.cityRun.CrUserTransactionRecordMapper;
import com.lemon.model.cityRun.CrShopOrderComment;
import com.lemon.model.cityRun.CrShopOrderGoods;
import com.lemon.model.cityRun.CrShopOrders;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrUserTransactionRecord;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: CrShopOrderService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月26日
 * @版本 V 1.0 
 */
@Service
public class CrShopOrderServiceImpl implements CrShopOrderService {

	@Resource
	private CrShopOrdersMapper oMapper;
	@Resource
	private CrShopOrderGoodsMapper gMapper;
	@Resource
	private CrShopOrderCommentMapper cMapper;
	@Resource
	private CrShopGoodsMapper goMapper;

	@Resource
	private CrShopUserMapper uMapper;

	@Resource
	private CrUserTransactionRecordMapper transMapper;
	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月26日
	 */
	@Override
	public List<CrShopOrders> selectListBySelective(CrShopOrders order) throws Exception {
		PageHelper.startPage(order.getPageNum() , order.getPageSize());
		return oMapper.selectListBySelective(order);
	}

	/**
	 * <p>
	 * Title: selectOrderGoodsListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param ogo
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月26日
	 */
	@Override
	public List<CrShopOrderGoods> selectOrderGoodsListBySelective(CrShopOrderGoods ogo) throws Exception {

		return gMapper.selectListBySelective(ogo);
	}

	/**
	 * <p>
	 * Title: selectOrderProfitToday
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月26日
	 */
	@Override
	public Map<String , Object> selectOrderProfitToday(CrShopOrders order) throws Exception {

		return oMapper.selectOrderProfitToday(order);
	}

	/**
	 * <p>
	 * Title: updateCommentBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param comment
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月1日
	 */
	@Override
	public int updateCommentBySelective(CrShopOrderComment comment) throws Exception {

		return cMapper.updateByPrimaryKeySelective(comment);
	}

	/**
	 * <p>
	 * Title: selectCommentsListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param comment
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月1日
	 */
	@Override
	public List<CrShopOrderComment> selectCommentsListBySelective(CrShopOrderComment comment) throws Exception {
		PageHelper.startPage(comment.getPageNum() , comment.getPageSize());
		List<CrShopOrderComment> list = cMapper.selectListBySelective(comment);
		return list;
	}

	/**
	 * <p>
	 * Title: getOrderProfitList
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param orders
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月5日
	 */
	//	@Cacheable(value = "city:porfit#3600#600" , keyGenerator = "keyGenerator" , unless = "#result == null")
	@Override
	public List<Map<String , Object>> getOrderProfitList(CrShopOrders orders) throws Exception {
		PageHelper.startPage(orders.getPageNum() , orders.getPageSize());
		return oMapper.selectOrderProfitOnDate(orders);
	}

	/**
	 * <p>
	 * Title: getShopsCommentList
	 * </p>
	 * <p>
	 * Description:(一句话阐述): wx查询商家评论列表
	 * </p>
	 * 
	 * @param shops
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月9日
	 */
	@Override
	public List<CrShopOrderComment> getShopsCommentList(CrShopOrderComment shops) throws Exception {
		PageHelper.startPage(shops.getPageNum() , shops.getPageSize());
		return cMapper.getShopsCommentList(shops);
	}

	/**
	 * <p>
	 * Title: getShopTodayFirgit
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param orders
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月10日
	 */
	@Override
	public Map<String , Object> getShopTodayFirgit(CrShopOrders orders) throws Exception {
		return oMapper.getShopTodayFirgit(orders);
	}

	/**
	 * <p>
	 * Title: createOrdersInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order
	 * @author Ran_cc
	 * @date 2018年11月12日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public void createOrdersInfo(CrShopOrders order) throws Exception {
		//先创建订单,  返回订单id
		oMapper.insertSelective(order);
		//将订单id存入购买的菜品中
		String ids = order.getGoodsIds();
		String[] split = ids.split(",");
		CrShopOrderGoods csog = null;
		List<CrShopOrderGoods> list2 = new ArrayList<CrShopOrderGoods>();
		for (int i = 0; i < split.length; i++) {
			csog = new CrShopOrderGoods();
			String id_num = split[i];
			String[] goodsinfo = id_num.split("-");
			csog.setGoodsId(goodsinfo[0]);
			csog.setGoodsNum(Integer.parseInt(goodsinfo[1]));
			csog.setGoodsPrice(Integer.parseInt(goodsinfo[2]));
			csog.setGoodsTitle(goodsinfo[3]);
			csog.setOrderId(IdTypeHandler.encode(Long.parseLong(order.getId())));
			csog.setStatus(1);
			list2.add(csog);
		}
		//批量插入订单商品
		gMapper.insertBatch(list2);
		//
		/*
		 * CrShopOrders order2 = new CrShopOrders();
		 * order2.setId(IdTypeHandler.encode(Long.parseLong(order.getId())));
		 * CrShopOrders orders = oMapper.selectBySelective(order2);
		 * orders.setList(list2);
		 * CrShopOrders or = new CrShopOrders();
		 * or.setShopId(orders.getShopId());
		 * or.setWhere(" and a.create_time >= ? " , CityRunUtils.getBusineDateString());
		 * int coun = oMapper.selectCountBySelective(or);
		 * orders.setSort(coun);
		 * //获取打印机
		 * // CrShopPrinter cp = new CrShopPrinter();
		 * // cp.setShopId(orders.getShopId());
		 * // cp.setStatus(1);
		 * //
		 * // List<CrShopPrinter> listPrinters = shopsService.selectPrintListBySelective(cp);
		 * // if (listPrinters.size() > 0) {
		 * // CrShopPrinter p = listPrinters.get(0);
		 * // String printer = PrinterUtils.printer(orders , p.getDevNo() , p.getDevSecretKey() , 1 , 1);
		 * // System.out.println(printer);
		 * // }
		 * String string = RedisUtil.get("cityRun:print:print_" + orders.getShopId());
		 * if (StringUtil.isNotBlank(string)) {
		 * JSONObject js = (JSONObject) JSONObject.parse(string);
		 * JSONArray jsonArray = js.getJSONArray("list");
		 * jsonArray.add(orders);
		 * js.put("list" , jsonArray);
		 * RedisUtil.set("cityRun:print:print_" + orders.getShopId() , js);
		 * } else {
		 * JSONObject js = new JSONObject();
		 * JSONArray arr = new JSONArray();
		 * arr.add(orders);
		 * js.put("list" , arr);
		 * RedisUtil.set("cityRun:print:print_" + orders.getShopId() , js);
		 * }
		 */
	}

	/**
	 * <p>
	 * Title: selectCountBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param or
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月12日
	 */
	@Override
	public int selectCountBySelective(CrShopOrders or) throws Exception {

		return oMapper.selectCountBySelective(or);
	}

	/**
	 * <p>
	 * Title: updateOrderInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param order
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月14日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int updateOrderInfo(CrShopOrders order) throws Exception {
		int i = oMapper.updateByPrimaryKeySelective(order);
		CrShopOrders orders = oMapper.selectByPrimaryKey(order.getId());

		if (orders.getStatus() == 4 && orders.getPayType() == 1) {
			CrShopUser u = new CrShopUser();
			u.setWhere(" and b.id = ?  " , IdTypeHandler.decode(orders.getShopId()));
			u = uMapper.selectBySelective(u);
			CrShopUser uu = new CrShopUser();
			uu.setId(u.getId());
			uu.setAmount(orders.getPayPrice());
			int selective = uMapper.updateUserAmount(uu);
			CrUserTransactionRecord tran = new CrUserTransactionRecord();
			tran.setShopUserId(u.getId());
			tran.setShopAccount(u.getUserAccount());
			tran.setShopTitle(orders.getShopTitle());
			tran.setShopId(orders.getShopId());
			tran.setSerialNumber(orders.getOrderNo());
			tran.setAmount(orders.getPayPrice());
			tran.setSerialNumber(StringUtil.createSerialNoCityRun());
			tran.setStatus(1);
			tran.setType(2);
			tran.setTranType(1);
			transMapper.insertSelective(tran);
		}

		return i;
	}

}
